<?php

Yii::import('application.models._base.BaseUserExtend');

class UserExtend extends BaseUserExtend
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function rules() {
        return array(
            array('uid', 'required'),
            array('uid, province, city, town', 'numerical', 'integerOnly'=>true),
            array('intro, address', 'length', 'max'=>255),
            array('real_name', 'length', 'max'=>20),
            array('phone, qq, birthday', 'length', 'max'=>15),
            array('weibo', 'length', 'max'=>40),
            array('astro', 'length', 'max'=>18),
            array('job', 'length', 'max'=>60),
            array('code', 'length', 'max'=>6),
            array('intro, real_name, phone, weibo, qq, birthday, astro, job, province, city, town, address, code', 'default', 'setOnEmpty' => true, 'value' => null),
            array('intro, uid, real_name, phone, weibo, qq, birthday, astro, job, province, city, town, address, code', 'safe', 'on'=>'search'),
        );
    }
    public function updateUserExtend($uid,$real_name,$phone,$weibo,$qq,$year,$month,$day,$astro,$job,$province,$city,$town,$address,$code){
        if(!$uid){
            return false;
        }
        $info = $this->findByPk($uid);
        if(!$info){
            return false;
        }
        $real_name ? $info->real_name = $real_name : true;
        $phone ? $info->phone = $phone : true;
        $weibo ? $info->weibo = $weibo : true;
        $qq ? $info->qq = $qq : true;
        if($year && $month && $day){
            $info->birthday = $year.'-'.$month.'-'.$day;
        }
        $astro ? $info->astro = $astro : true;
        $job ? $info->job = $job : true;
        $province ? $info->province = $province : true;
        $city ? $info->city = $city :true;
        $town ? $info->town = $town : true;
        $address ? $info->address = $address : true;
        $code ? $info->code = $code : true;
        $ok = $info->save();
        if($ok){
            return true;
        }else{
            return false;
        }
    }
    public function getUserExtend($uid){
        $user_extend = Yii::app()->db->createCommand()
            ->from('{{user_extend}}')
            ->where('uid=:id', array(':id'=>$uid))
            ->queryRow();
        if(!$user_extend){
            $ue_db = new UserExtend();
            $ue_db->uid = $uid;
            $ue_db->save();
            $user_extend = Yii::app()->db->createCommand()
                ->from('{{user_extend}}')
                ->where('uid=:id', array(':id'=>$uid))
                ->queryRow();
        }
        return $user_extend;
    }
}